import java.util.Arrays;

public class MyStack {
    public int[] arr;
    public int usedSize;

    public MyStack() {
        arr = new int[10];
    }

    public int push(int data){
        if(isFull()){
            arr = Arrays.copyOf(arr,arr.length + 10);
        }
        arr[usedSize] = data;
        usedSize++;
        return data;
    }

    private boolean isFull(){
        return usedSize == arr.length;
    }

    public boolean isEmpty(){
        return usedSize == 0;
    }
    public int pop(){
        if(isEmpty()){
            throw new EmptyStackException("The Stack is empty!");
        }
//        int val = usedSize-1;
//        usedSize--;
//        return arr[val];
        //又或者写成如下更加简便的形式：
        return arr[--usedSize];
    }

    public int peek(){
        if(isEmpty()){
            throw new EmptyStackException("The Stack is empty!");
        }
        return arr[usedSize-1];
    }

    public int size(){
        return usedSize;
    }
}
